package com.ykdl.member.kid.gears;

import java.util.ArrayList;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.DoctorSheetListAdapter;
import com.ykdl.member.kid.adapters.TopicSheetListAdapter;
import com.ykdl.member.kid.beans.TopicSheetList;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.views.PullToRefreshView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 多个话题（专题列表）列表页
 *
 */
public class TopicsSheetListActivity extends BaseFragmentActivity implements OnClickListener {
	private ImageView iv_left_back;
	private TextView tv_topic_sheet_title;
	private PullToRefreshView pullToResreshView;
	private ListView lv_topic_sheet_list;
	private String catalog_id;
	private HttpBaseRequest request;
	private TopicSheetList catalog;
	private TextView tv_topic_sheet_intro_content,tv_topic_sheet_intro_title;
	private ImageView iv_topic_sheet_intro_image;
	private View view;
	private BitmapUtils bitmapUtils;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		bitmapUtils = new BitmapUtils(this);
		getIntentData();
		
		initData();
		setContentView(R.layout.layout_topic_sheet_list_activity);
		initView();
	}

	private void getIntentData() {
		catalog_id = this.getIntent().getStringExtra("catalog_id");
	}

	private void initData() {
		showProgress(this);
		final String url = KidConfig.BASE_TEST_SERVER + "catalogs/" + catalog_id;
		request = Wxxr.getInstance().getRequest(url, null);
		TaskManager.startHttpRequest(request, new TopicSheetListTag(), TopicSheetList.class);
	}
	class TopicSheetListTag implements ITag{
		@Override
		public void refreshUI(Object t, DataState ds) {
			if(null != t && t instanceof TopicSheetList){
				catalog = (TopicSheetList)t;
				if(catalog.getCatalog()!= null){
					showData();
				}
				dismissProgress();
			}
		}
		@Override
		public boolean refreshError(DataParseError parseError) {
			dismissProgress();
			return false;
		}
		@Override
		public void refreshCancel() {
		}
	}
	
	private void showData() {
		//设置title
		final String intro = catalog.getCatalog().getIntro();
		
		if(catalog.getCatalog().getTitle().length() > 4){
			tv_topic_sheet_title.setText(catalog.getCatalog().getTitle().substring(0, 4)+ "...");
		}else{
			tv_topic_sheet_title.setText(catalog.getCatalog().getTitle());
		}
		
		tv_topic_sheet_intro_title.setText(catalog.getCatalog().getTitle());
		
		//设置专题介绍
		tv_topic_sheet_intro_content.setText(intro !=null && !"".equals(intro)?intro:"");
		
		//专题图标
		if(catalog.getCatalog().getFile_meta() != null && !"".equals(catalog.getCatalog().getFile_meta())){
			if(catalog.getCatalog().getFile_meta().getDownload_urls() !=null && catalog.getCatalog().getFile_meta().getDownload_urls().getBig() != null){
				bitmapUtils.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
				bitmapUtils.configDefaultLoadFailedImage(R.drawable.iask_main_middle_img);
				bitmapUtils.display(iv_topic_sheet_intro_image, catalog.getCatalog().getFile_meta().getDownload_urls().getBig().getUrl());
			}else{
				iv_topic_sheet_intro_image.setImageResource(R.drawable.iask_main_middle_img);
			}
		}else{
			iv_topic_sheet_intro_image.setImageResource(R.drawable.iask_main_middle_img);
		}
		
		lv_topic_sheet_list.addHeaderView(view);
		
		if(catalog.getCatalog().getCatalog_type() == 1 && catalog.getCatalog().getTag().equals("groups")){//普通群组专题
			if(null != catalog.getCatalog()){
				TopicSheetListAdapter  adapter = new TopicSheetListAdapter(this, bitmapUtils);
				adapter.setData((ArrayList<CatalogItemBean>)catalog.getCatalog().getCatalogItems());
				lv_topic_sheet_list.setAdapter(adapter);
			}
		}else if(catalog.getCatalog().getCatalog_type() == 2 && catalog.getCatalog().getTag().equals("clinic")){//医生专题
			if(null != catalog.getCatalog()){
				DoctorSheetListAdapter  adapter = new DoctorSheetListAdapter(this, bitmapUtils);
				if (catalog.getCatalog().getCatalogItems()!=null) {
					adapter.setData((ArrayList<CatalogItemBean>)catalog.getCatalog().getCatalogItems());
				}
				lv_topic_sheet_list.setAdapter(adapter);
			}
		}
		
		
		lv_topic_sheet_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if(arg2 == 0)return;
				CatalogTopicItem  topic = (CatalogTopicItem)catalog.getCatalog().getCatalogItems().get(arg2-1);
				Intent intent =new Intent(TopicsSheetListActivity.this,TopicAndResultActivity.class);
				intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, topic.getItem().getTopic_id());
				intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, topic.getItem().getSource_id());
				TopicsSheetListActivity.this.startActivity(intent);
			}
			
		});
	}

	private void initView() {
		
		iv_left_back = (ImageView) this.findViewById(R.id.iv_left_back);
		iv_left_back.setOnClickListener(this);
		tv_topic_sheet_title = (TextView) this.findViewById(R.id.tv_topic_sheet_title);
		pullToResreshView = (PullToRefreshView) this.findViewById(R.id.pullToResreshView);
		pullToResreshView.setEnablePullTorefresh(false);
		pullToResreshView.setEnablePullLoadMoreDataStatus(false);
		pullToResreshView.setOnHeaderRefreshListener(null);
		pullToResreshView.setOnFooterRefreshListener(null);
		lv_topic_sheet_list = (ListView) this.findViewById(R.id.lv_topic_sheet_list);
		initHeadView();
	}
	private void initHeadView(){
		view = LayoutInflater.from(this).inflate(R.layout.layout_topic_sheet_lv_head_view, null);
		//专题介绍图片
		iv_topic_sheet_intro_image = (ImageView) view.findViewById(R.id.iv_topic_sheet_intro_image);
		//专题介绍内容
		tv_topic_sheet_intro_content = (TextView) view.findViewById(R.id.tv_topic_sheet_intro_content);
		tv_topic_sheet_intro_title = (TextView) view.findViewById(R.id.tv_topic_sheet_intro_title);
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_left_back:
			this.finish();
			break;
		default:
			break;
		}
	}
	
	
}
